// pages/chat/cityGroup/index.js
var basePage=require('../../../common/BasePage')

Page({
behaviors:[basePage],
    /**
     * 页面的初始数据
     */
    data: {
        article_content:null,
        sortIndex: 0, //分类角标
        sortList: [],
        addCart:false,
        sku: 1, //可以加入购物车得最大数量
        nums: 1, //购买产品数量
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getArticle()
        this.getNewSort()
    },
    getArticle() {
        this.request({
            url: this.data.host + '/home/article/detail?article_id=91'
        }, this.get_detail_ok);
    },
    get_detail_ok(res) {
        console.log("get_detail_ok", res);
        this.setData({
            article_content: res.data.article_content.content.replace(/<img /ig, '<img width="100%" ')
        });
    },
    toGroupList(){
        wx.navigateTo({
          url: '/pages/chat/cityGroup/list',
        })
    },
    //查询分类
    getNewSort() {
        this.request({
            url: this.data.host + "/home/product/indexCate",
            data: {
                wh_id: 1
            }
        }, res => {
            let list = res.data
            list.unshift({
                title: "全部",
                cate_id: ""
            })
            this.setData({
                sortList: list
            })
            this.getFlowers()
        })
    },
    //切换分类
    sort_change(e) {
        // console.log("e", e)
        this.setData({
            sortFlowerloading: true,
            sortIndex: e.detail.index,
            // current_page:0,
            // pages:1,
            // sortFlowerList:[]
        })
        this.getFlowers()
    },
    getFlowers() {

        var sortList = this.data.sortList
        // if (this.data.current_page == this.data.pages) {
        // 	return console.log("没有更多了");
        // }

        this.request({
            url: this.data.host + "/home/supplier/product/index",
            data: {
                limit: 4,
                wh_id: 1,
                cate_id: sortList[this.data.sortIndex].cate_id,
                // page: (this.data.current_page * 1) + 1,
            }
        }, res => {
            res.data.map(val => {
                val.thumb = this.get_thumb(val.product_img);
                // console.log("val.thumb",val.thumb);
            });
            // res.current_page == 1 ? list = res.data : list = list.concat(res.data);
            this.setData({
                sortFlowerList: res.data,
                sortFlowerloading: false
                // current_page:res.current_page,
                // pages:res.pages
            })
        })
    },

    /**
     * 添加购物车按钮点击事件
     * @param {*} param0 
     */
    add_cart_click(e) {
        let pdc = e.detail;
        // console.log("秒杀", pdc)
        this.setData({
            nums: 1,
            addCart: true,
            pdc: pdc,
            sku: pdc.sku,
            product_id: pdc.pid,
            product_name: pdc.product_name,
            carTitle: e.currentTarget.dataset.titlt

        });
    },

    onCloseCart() {
        this.setData({
            addCart: false
        })
    },
    /**
     * 添加产品到购物车
     */
    add_cart() {
        let data = {
            pid: this.data.product_id,
            nums: this.data.nums,
        }
        let pdc = this.data.pdc
        if (pdc.is_seckill == 1 && this.data.carTitle == "整点秒杀") { //秒杀
            data.from = "seckill"
        } else {
            data.from = "flower"
        }

        this.request({
            url: this.data.host + '/home/cart/addToCart',
            data: data,
            method: 'post'
        }, this.add_cart_ok);

    },
    //立即购买
    buy_good() {
        console.log("立即购买", this.data.pdc)
        let data = {
            pid: this.data.product_id,
            nums: this.data.nums,
            wh_id: 1
        }
        let pdc = this.data.pdc
        if (pdc.is_seckill == 1 && this.data.carTitle == "整点秒杀") { //秒杀
            data.from = "seckill"
        } else {
            data.from = "flower"
        }

        this.request({
            url: this.data.host + '/home/flower/preInfo',
            data: data,
            method: 'get'
        }, res => {
            this.setData({
                addCart: false
            })
            res.data.cashier_decca = this.get_thumb(res.cashier_decca, 'wide');
            let that = this;
            wx.navigateTo({
                // url: '/pages/order_confirm/order_confirm',
                url: '/pages/neworder_confirm/order_confirm',
                success: function (obj) {
                    // 通过eventChannel向被打开页面传送数据

                    obj.eventChannel.emit('orderData', {
                        wh_id: 1,
                        // index: that.data.areaData[that.data.tabIndex].index,
                        data: res.data,
                        m_addr: res.m_addr,
                        activeCar: data.from,
                        wuliu: res.data.freight_list,
                        specialList: res.data.extra_list,
                        pack_list: res.data.pack_list,
                        buyType: 1, //1立即购买0购物车
                        order_pei_type: res.order_pei_type,
                        orderWarehouse: res.warehouse
                    })
                }
            })
        });
    },

    /**
     * 添加购物车结果处理
     * @param {*} res  后台返回的数据
     */
    add_cart_ok(res) {
        // console.log("add_cart_ok",res);
        // this.setData({nums:1})
        this.setData({
            addCart: false
        })
        wx.showToast({
            title: res.msg,
            icon: 'success'
        })
    },
    nums_change(e) {
        // this.data.nums = e.detail;
        this.setData({
            nums: e.detail
        })
        // console.log("nums_change", e)
    },

    toList() {
        wx.switchTab({
          url: '/pages/cat/Index',
        })
    },
    
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})